package base.facebook {
	import base.loader.URLLoaderExt;
	import base.Singleton;
	import flash.events.EventDispatcher;
	import base.mvc.EventModel;
	import com.facebook.graph.Facebook;
	import com.facebook.graph.data.FacebookSession;
	import flash.external.ExternalInterface;
	import base.loader.LoaderJSON;
	import base.loader.LoaderJSONEvent;
	
	public class FacebookGateway extends Singleton {
		
		private static var modelDispatcher: EventDispatcher = new EventDispatcher();
		
		public static function addEventListener(type: String, func: Function, priority: int = 0): void {
			FacebookGateway.modelDispatcher.addEventListener(type, func, false, priority);
		}
	
		public static function removeEventListener(type: String, func: Function): void {
			FacebookGateway.modelDispatcher.removeEventListener(type, func);
		}
		
		public static function dispatchEvent(type: String, data: * = null): void {
			FacebookGateway.modelDispatcher.dispatchEvent(new EventModel(type, data));
		}
		
		public static const ON_LOGIN_SUCCESS: String = "onLoginSuccess";
		public static const ON_LOGIN_ERROR: String = "onLoginError";
		public static const ON_GET_USER_SUCCESS: String = "onGetUserSuccess";
		public static const ON_GET_USER_ERROR: String = "onGetUserError";
		
		public static var facebook: Facebook;
		public static var idLoggedUser: String = ""; 
		public static var isNotLogged: Boolean;
		public static var logWhenNotLogged: Boolean;
		
		public static function init(idApplication: String, isNotLogged: Boolean = true, logWhenNotLogged: Boolean = false): void {
			if (idApplication) {
				FacebookGateway.isNotLogged = isNotLogged;
				FacebookGateway.logWhenNotLogged = logWhenNotLogged;
				Facebook.init(idApplication, FacebookGateway.onInit);
			} else FacebookGateway.dispatchEvent(FacebookGateway.ON_LOGIN_ERROR);
		}
		
		public static function onInit(session: FacebookSession, fail:Object):void {
			if (FacebookGateway.isNotLogged) {
				if (FacebookGateway.logWhenNotLogged) ExternalInterface.call('redirect'); //logowanie z autentyfikacją
				else FacebookGateway.dispatchEvent(FacebookGateway.ON_LOGIN_ERROR);
			} else {
				if (!session) ExternalInterface.call('redirect'); //tylko autentyfikacja
				else {
					FacebookGateway.idLoggedUser = session.uid;
					FacebookGateway.dispatchEvent(FacebookGateway.ON_LOGIN_SUCCESS, session);
				}
			}
		}
		
		private static var idUserToGet = "";
		private static var loaderJSONUser: LoaderJSON;
		
		public static function getUser(idUser: String = ""): void {
			FacebookGateway.idUserToGet = idUser || FacebookGateway.idLoggedUser;
			Facebook.api('/' + FacebookGateway.idUserToGet, FacebookGateway.onGetUser);
		}
		
		private static function onGetUser(response: Object, fail: Object):void {
			if (response) FacebookGateway.dispatchEvent(FacebookGateway.ON_GET_USER_SUCCESS, response);
			else {
				FacebookGateway.loaderJSONUser =  new LoaderJSON("http://graph.facebook.com/" + FacebookGateway.idUserToGet);
				FacebookGateway.loaderJSONUser.addEventListener(LoaderJSONEvent.JSON_LOADED, FacebookGateway.jsonUserLoaded);
				FacebookGateway.loaderJSONUser.addEventListener(LoaderJSONEvent.JSON_NOT_LOADED, FacebookGateway.jsonUserNotLoaded);
				FacebookGateway.loaderJSONUser.readJSON();
			}
		}
		
		private static function jsonUserLoaded(e: LoaderJSONEvent): void {
			FacebookGateway.dispatchEvent(FacebookGateway.ON_GET_USER_SUCCESS, FacebookGateway.loaderJSONUser.data);
		}
		
		private static function jsonUserNotLoaded(e: LoaderJSONEvent): void {
			FacebookGateway.dispatchEvent(FacebookGateway.ON_GET_USER_ERROR);
		}
		
		public static function setCanvasSize(width: Number, height: Number):void {
			Facebook.setCanvasSize(width, height);
		}
		
	}

}